package
{
	import UIFree.CLoader;
	import UIFree.uif_Timer;
	
	import UIUtils.uif_util;
	
	import com.adobe.serialization.json.JSON;
	
	import data.cd_clothing;
	import data.cd_hairs;
	import data.checkTmpl;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	
	[SWF(width="1024", height="800", backgroundColor="#FFFFFF")]
	public class pj_checkTmpl extends Sprite {
		public static const BASEURL: String = 'http://clothing.1gb.ru/clothing/hair';
		public static const TMPL_URL: String = BASEURL + '/JPG/';
		public static const PREVIEW_URL: String = BASEURL + '/jpg_preview/';
		
		private var _loader: Loader;
		private var lc: LoaderContext;
		private var _trace: TextField;
		private var _curLoad: int;
		private var r_items: Array;
		public function pj_checkTmpl() {
			super();
			lc = new LoaderContext(false);
			
			_trace = new TextField();
			_trace.width = stage.stageWidth;
			_trace.height = stage.stageHeight;
			
			uif_util.applyTextFormat(_trace, {
				font: 'arial',
				size: 11,
				wordWrap: true,
				color: 0,
				multiline: true
			});
			addChild(_trace);
			
			reset();
		}
		
		protected function order(): void {
			var result: Array = checkTmpl.list;
			result = result.sortOn('id');
			_trace.appendText(String(result.length) + '\n');
			result.forEach(function(item: Object, i: int, arr: Array): void {
				_trace.appendText(JSON.encode(item) + ',\n');
			});
		}
		
		protected function reset(): void {
			var result: Array = cd_hairs.list;
			result = result.sortOn('id');
			resetData(result, result, 11260);
		}
		
		protected function resetData(items: Array, result: Array, startID: int): void {
			r_items = new Array();
			items.forEach(function(item: Object, i: int, arr: Array): void {
				if (item.id >= startID) r_items.push(item);
			});	
			
			_trace.appendText('---' + r_items.length + '---\n');
			
			_curLoad = -1;
			loadNext();
		}
		
		protected function loadNext(): void {
			_curLoad++;
			if (r_items.length > _curLoad) {
				var wait: uif_Timer = uif_Timer.one(10000, function(): void {
					loadNext();
				});
				checkTmp(r_items[_curLoad], function(a_item: Object): void {
					_trace.appendText(JSON.encode(a_item) + ',\n');
					wait.stop();
					loadNext();
				})
			} else _trace.appendText('---STOP---');
		}
		
		protected function checkTmp(item: Object, doCompleteTmpl: Function): void {
			var count: int = 0;
			
			function doComplete(e: Event): void {
				count++;
				if (count >= 3) doCompleteTmpl(item);
			}
			
			new CLoader(null, TMPL_URL + item.id + '.jpg', doComplete, 0, 0, lc);
			new CLoader(null, TMPL_URL + item.id + 'm.jpg', doComplete, 0, 0, lc);
			new CLoader(null, PREVIEW_URL + 'i' + item.id + '.jpg', doComplete, 0, 0, lc);
		}
	}
}